<template>
  <div>
    <a-row :gutter="20">
      <a-col :span="8">
        <a-input-search
          placeholder="请输入内容"
          enter-button
          v-model="queryInfo.query"
          @search="getSearchOrderList"
        >
        </a-input-search>
      </a-col>
    </a-row>
    <br />
    <a-table
      align="center"
      rowKey="order_id"
      :columns="columns"
      :data-source="orderlist"
      :pagination="pagination"
      bordered
    >
      <a-tag
        slot="pay_status"
        slot-scope="pay_status"
        v-if="pay_status === '1'"
        color="cyan"
        >已付款
      </a-tag>
      <a-tag color="red" v-else>未付款</a-tag>
      <template slot="create_time" slot-scope="create_time">
        {{ create_time | dateFormat }}
      </template>
      <template slot="operation">
        <!-- 修改订单按钮 -->
        <a-button
          icon="edit"
          style="margin:3px;background-color:#409EFF;color:#fff;width:44px;heigt:30px"
          @click="showBox"
        >
        </a-button>
        <!-- 物流信息按钮 -->
        <a-button
          icon="environment"
          style="margin:3px;background-color:#67C23A;color:#fff;width:44px;heigt:30px"
          @click="showProgressBox"
        >
        </a-button>
      </template>
    </a-table>
    <!-- 修改地址的对话框 -->
    <a-modal
      title="修改地址"
      :visible="addressVisible"
      width="50%"
      ok-text="确认"
      cancel-text="取消"
      @cancel="addressDialogClosed"
      @ok="addressVisible = false"
    >
      <a-form-model
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        laba-width="100px"
      >
        <a-form-model-item label="省市区/县" prop="address1">
          <a-cascader
            :options="cityData"
            expand-trigger="hover"
            placeholder="请选择"
            v-model="addressForm.address1"
            clearable
          ></a-cascader>
        </a-form-model-item>
        <a-form-model-item label="详细地址" prop="address2">
          <a-input v-model="addressForm.address2"></a-input>
        </a-form-model-item>
      </a-form-model>
      <span slot="footer" class="dialog-footer">
        <a-button @click="addressVisible = false">取 消</a-button>
        <a-button type="primary" @click="addressVisible = false"
          >确 定</a-button
        >
      </span></a-modal
    >

    <!-- 展示物流进度的对话框 -->
    <a-modal
      title="物流进度"
      :visible="progressVisible"
      ok-text="确认"
      cancel-text="取消"
      @ok="progressVisible = false"
      @cancel="progressVisible = false"
    >
      <!-- 时间线 -->
      <a-timeline>
        <a-timeline-item v-for="(activity, index) in progressInfo" :key="index">
          {{ activity.context }}
          <br />
          {{ activity.time }}
        </a-timeline-item>
      </a-timeline>
    </a-modal>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderlist: [],
      addressVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      cityData,
      progressVisible: false,
      // 物流信息
      progressInfo: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '订单编号',
          dataIndex: 'order_number'
        },
        {
          title: '订单价格(元)',
          dataIndex: 'order_price'
        },
        {
          title: '是否付款',
          dataIndex: 'pay_status',
          scopedSlots: { customRender: 'pay_status' }
        },
        {
          title: '是否发货',
          dataIndex: 'is_send'
        },
        {
          title: '下单时间',
          dataIndex: 'create_time',
          scopedSlots: { customRender: 'create_time' }
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      pagination: {
        position: 'bottom',
        // 总数据条数
        total: 0,
        current: 1,
        pageSize: 5,
        showSizeChanger: true,
        pageSizeOptions: ['1', '2', '5', '10', '20'],
        showTotal: total => `总共 ${total} 条`,
        onChange: (current, pageSize) => this.pageChange(current, pageSize),
        onShowSizeChange: (current, size) => this.showSizeChange(current, size)
      }
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      // console.log(res)
      this.pagination.total = res.data.total
      this.orderlist = res.data.goods
      this.pagination.current = this.queryInfo.pagenum
      this.pagination.pageSize = this.queryInfo.pagesize
    },
    getSearchOrderList() {
      this.queryInfo.pagenum = 1
      this.getOrderList()
    },
    // 监听 current 当前页 pagesize页面大小 改变的事件
    pageChange(current, pageSize) {
      this.queryInfo.pagenum = current
      this.getOrderList()
    },
    // 改变每页显示条数
    showSizeChange(current, size) {
      this.queryInfo.pagesize = size
      this.pagination.pageSize = size
      this.getOrderList()
    },
    // 展示修改地址的对话框
    showBox() {
      this.addressVisible = true
    },
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败！')
      }
      this.progressInfo = res.data
      this.progressVisible = true
      // console.log(this.progressInfo)
    }
  }
}
</script>

<style></style>
